﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.CollectControl
{
    public class PresenterSearchPayment
    {
        ISearchPaymentView view;
        ICollection<Model.Payment> payments;

        public PresenterSearchPayment(ISearchPaymentView view)
        {
            this.view = view;
        }

        public Dictionary<string, ICollection<Model.Allotment>> LlenarCbx()
        {
            Dictionary<string, ICollection<Model.Allotment>> manzanos = new Dictionary<string, ICollection<Model.Allotment>>();
            CleanSearch();

            List<Model.Square> squareList = ManagerSquare.GetSquares().ToList();

            squareList = squareList.OrderBy(x => x.SquareId).ToList();

            List<Model.Allotment> activeAllotments = ManagerAllotment.SearchActiveAllotments();

            foreach (Model.Square square in squareList)
            {
                var filteredAllotmentsBySquares = activeAllotments.FindAll(a => a.SquareId.Equals(square.SquareId));
                manzanos.Add(square.SquareId.ToString(), filteredAllotmentsBySquares);
            }

            return manzanos;
        }

        public void VerTotal()
        {
            payments = view.PaymentFinded;
            decimal total = 0;

            foreach (Model.Payment c in payments)
                total += c.TotalAmount;

            view.TotalAmount = total;
        }

        public bool VerifyDates()
        {
            try
            {
                Model.Payment p = new Model.Payment();

                p.InitialDate = view.InitialDate;
                p.FinalDate = view.FinalDate;
                ManagerCollectControl.VerifyDates(p);

                //p.FinalDate = view.FinalDate.AddDays(1);

                return true;
            }
            catch (Exception ex)
            {
                view.NotifyErrorMessage(string.Format(ex.Message, "Buscar Cobros"));
                //.ErrorMessage
                return false;
            }
        }

        public void SearchPayments()
        {
            bool datosCompletos = true;
            Model.Payment payment = new Model.Payment();
            //ICollection<Model.Payment> payments;

            CleanSearch();

            if (VerifyDates())
            {
                try
                {
                    Model.CollectControl c = new Model.CollectControl();
                    Model.Kardex k = new Model.Kardex();
                    Model.Allotment a = new Model.Allotment();
                    //CleanSearch();

                    try
                    {
                        a.AllotmentName = view.Lote;
                        a.SquareId = view.Manzano;

                        a = ManagerKardex.GetAllotmentByAllotmentNameAndSquareId(a);

                        if (k == null)
                            datosCompletos = false;
                        else
                        {
                            c.KardexId = a.KardexId.Value;
                            k.KardexId = c.KardexId;
                            k = ManagerKardex.GetKardexByKardexId(k);
                        }

                        payment.KardexId = k.KardexId;
                        
                        
                        
                        
                        //payment.KardexId = view.ClientId;





                    }
                    catch (Exception ex)
                    {
                        datosCompletos = false;
                    }

                    payment.InitialDate = view.InitialDate;
                    payment.FinalDate = view.FinalDate.AddDays(1);

                    if (datosCompletos)
                    {
                        view.PaymentFinded = ManagerCollectControl.SearchPaymentByDatesAndClientId(payment);
                        view.NombreSocio = k.Person.Names + " " + k.Person.LastNames;
                        view.CiSocio = k.Person.PersonId;
                    }
                    else
                    {

                        //view.PaymentFinded = ManagerCollectControl.SearchPaymentByDatesMinusClientId(payment);
                        throw new Exception("No se encontraron resultados de la busqueda...");
                    }

                    VerTotal();
                }
                catch (Exception ex)
                {
                    CleanSearch();
                    view.NotifyErrorMessage(ex.Message);
                }
            }
            else
                CleanSearch();
        }

        private void CleanSearch()
        {
            view.TotalAmount = 0;
            view.PaymentFinded = null;
        }

    }
}
